<template>
  <div class="goods" v-if="CategoryAll.length >= 0">
    <div class="tt">
      <span>首页</span>
      <i class="el-icon-arrow-right"></i>
      <span>全部商品</span>
      <i class="el-icon-arrow-right"></i>
      <span v-if="this.$route.query.search == undefined">分类</span>
      <span v-else>搜索</span>
    </div>
    <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
      <el-tab-pane disabled label="分类"></el-tab-pane>
      <el-tab-pane
        v-for="ii in CategoryAll"
        :key="ii.category_id"
        :label="ii.category_name"
        :disabled="falg"
      >
        <goodslist
          class="goodslist"
          v-for="item in ProductAll"
          :key="item.product_id"
          :list="item"
        ></goodslist>
        <el-pagination
          background
          layout="prev, pager, next"
          :total="total"
          :page-size="pageSize"
          hide-on-single-page
          @current-change="currentChange"
        >
        </el-pagination>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>
<script>
import goodslist from "../../components/mylist/index.vue";
export default {
  components: {
    goodslist,
  },
  data() {
    return {
      ProductAll: [],
      CategoryAll: [],
      activeName: "1",
      total: 0,
      pageSize: 15,
      falg: false,
    };
  },
  methods: {
    async getProduct() {
      if (this.$route.query.search != undefined) {
        return;
      }
      const res = await this.$http.getProduct();
      // console.log(res.data);
      this.total = res.data.length;
      const ress = await this.$http.getPagegetProduct({
        page: 1,
        limit: 15,
      });
      this.ProductAll = ress.data;
    },
    async getCategory() {
      const res = await this.$http.getCategory();
      // console.log(res.data);
      const aa = {
        category_id: 0,
        category_name: "全部",
      };
      res.data.unshift(aa);
      this.CategoryAll = res.data;
      // console.log(this.CategoryAll);
    },
    async handleClick(tab, event) {
      if (this.$route.query.search != undefined) {
        return;
      }
      // console.log(tab.index - 1);
      if (tab.index - 1 == 0) {
        this.getProduct();
        return;
      }
      // this.getProduct();
      const res = await this.$http.getProduct();
      const nlist = res.data.filter((item) => {
        return item.category_id == tab.index - 1;
      });
      this.ProductAll = nlist;
      this.total = this.ProductAll.length;
      // console.log(this.ProductAll);
    },
    // 返回顶部
    backtop() {
      const timer = setInterval(function () {
        const top =
          document.documentElement.scrollTop || document.body.scrollTop;
        const speed = Math.floor(-top / 5);
        document.documentElement.scrollTop = document.body.scrollTop =
          top + speed;

        if (top === 0) {
          clearInterval(timer);
        }
      }, 20);
    },
    async currentChange(currentPage) {
      console.log(currentPage);
      const res = await this.$http.getPagegetProduct({
        page: currentPage,
        limit: 15,
      });
      console.log(res.data);
      this.ProductAll = res.data;
      this.backtop();
    },
    async search() {
      console.log(this.$route.query.search);
      const res = await this.$http.getSearch(this.$route.query.search);
      this.ProductAll = res.data;
      if (this.$route.query.search == undefined) {
        this.falg = false;
      } else {
        this.falg = true;
      }
    },
  },
  created() {
    this.search();
    this.getProduct();
    this.getCategory();
  },
};
</script>
<style lang="scss">
.goods {
  width: 1225px;
  margin: 0 auto !important;
}
.tt {
  margin-bottom: 20px;
  span {
    height: 30px;
    line-height: 30px;
    color: #333;
    margin: 0 10px;
    font-size: 16px;

    &:first-child {
      font-weight: 700;
    }
  }
  i {
    color: #ccc;
  }
}
#tab-0 {
  font-size: 16px;
  font-weight: 800;
  color: #333;
}
.el-tab-pane {
  background: #fff;
  border-radius: 5px;
  padding: 20px 0;
}
.el-col {
  width: 20%;
}
.el-card {
  margin: 0 auto;
  width: 200px;
  cursor: pointer;
  transform: translate3d(0, -2px, 0);
  &:hover {
    box-shadow: 0 15px 30px rgba(0, 0, 0, 0.1) !important;
  }
}
.el-pagination {
  margin-top: 20px;
  width: 100%;
  text-align: center;
}
.el-tab-pane {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  .goodslist {
    margin-bottom: 40px;
    width: 20%;
    display: flex;
    justify-content: center;
  }
}
</style>
